2020-2021 Sunseeker Telemetry and Lighting System
adc10_b_ex2_intRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
70 //******************************************************************************
71 
72 #define TIMER_PERIOD 80
73 
74 void main (void)
75 {
76  //Stop Watchdog Timer
77  WDT_A_hold(WDT_A_BASE);
78 
79  //Set P1.1 as an input pin.
80  /*
81  * Select Port 1
82  * Set Pin 1 as input
83  * Set Ternary module function
84  */
85  GPIO_setAsPeripheralModuleFunctionInputPin(
86  GPIO_PORT_P1,
87  GPIO_PIN1,
88  GPIO_TERNARY_MODULE_FUNCTION);
89 
90  //Set P1.0 as an output pin.
91  /*
92  * Select Port 1
93  * Set Pin 0 as output
94  */
95  GPIO_setAsOutputPin(
96  GPIO_PORT_P1,
97  GPIO_PIN0);
98 
99 
100  //Set P3.5 as an output pin.
101  /*
102 
103  * Select Port 3
104  * Set Pin 5 as output
105  */
106  GPIO_setAsOutputPin(
107  GPIO_PORT_P3,
108  GPIO_PIN5);
109 
110  //Initialize the ADC10 Module
111  /*
112  * Base Address for the ADC10 Module
113  * Use internal ADC10 bit as sample/hold signal to start conversion
114  * USE MODOSC 5MHZ Digital Oscillator as clock source
115  * Use default clock divider of 1
116  */
117  ADC10_B_init(ADC10_B_BASE,
118  ADC10_B_SAMPLEHOLDSOURCE_SC,
119  ADC10_B_CLOCKSOURCE_ADC10OSC,
120  ADC10_B_CLOCKDIVIDER_1);
121 
122  ADC10_B_enable(ADC10_B_BASE);
123 
124  /*
125  * Base Address for the ADC10 Module
126  * Sample/hold for 16 clock cycles
127  * Do not enable Multiple Sampling
128  */
129  ADC10_B_setupSamplingTimer(ADC10_B_BASE,
130  ADC10_B_CYCLEHOLD_16_CYCLES,
131  ADC10_B_MULTIPLESAMPLESDISABLE);
132 
133  //Configure Memory Buffer
134  /*
135  * Base Address for the ADC10 Module
136  * Use input A1
137  * Use positive reference of Internally generated Vref
138  * Use negative reference of AVss
139  */
140  ADC10_B_configureMemory(ADC10_B_BASE,
141  ADC10_B_INPUT_A1,
142  ADC10_B_VREFPOS_INT,
143  ADC10_B_VREFNEG_AVSS);
144 
145  ADC10_B_clearInterrupt(ADC10_B_BASE,
146  ADC10IE0);
147  //Enable Memory Buffer interrupt
148  ADC10_B_enableInterrupt(ADC10_B_BASE,
149  ADC10IE0);
150 
151  //Configure internal reference
152  //If ref generator busy, WAIT
153  while ( REF_BUSY == Ref_isRefGenBusy(REF_BASE) ) ;
154 
155  //Select internal ref = 1.5V
156  Ref_setReferenceVoltage(REF_BASE,
157  REF_VREF1_5V);
158  //Internal Reference ON
159  Ref_enableReferenceVoltage(REF_BASE);
160 
161  // Configure TA0 to provide delay for reference settling ~75us
162  Timer_A_initUpModeParam initUpParam = {0};
163  initUpParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
164  initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
165  initUpParam.timerPeriod = TIMER_PERIOD;
166  initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
167  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
168  TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
169  initUpParam.timerClear = TIMER_A_DO_CLEAR;
170  initUpParam.startTimer = true;
171  Timer_A_initUpMode(TIMER_A1_BASE, &initUpParam);
172 
173  __bis_SR_register(CPUOFF + GIE); // LPM0, TA0_ISR will force exit
174 
175 
176 
177  for (;;)
178  {
179  //Delay between conversions
180  __delay_cycles(5000);
181 
182  //Enable and Start the conversion
183  //in Single-Channel, Single Conversion Mode
184  ADC10_B_startConversion(ADC10_B_BASE,
185  ADC10_B_SINGLECHANNEL);
186 
187  //LPM0, ADC10_ISR will force exit
188  __bis_SR_register(CPUOFF + GIE);
189  //For debug only
190  __no_operation();
191 
192 
193  }
194 }
195 
196 //ADC10 interrupt service routine
197 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
198 #pragma vector=ADC10_VECTOR
199 __interrupt
200 #elif defined(__GNUC__)
201 __attribute__((interrupt(ADC10_VECTOR)))
202 #endif
203 void ADC10_ISR (void)
204 {
205  switch (__even_in_range(ADC10IV,12)){
206  case 0: break; //No interrupt
207  case 2: break; //conversion result overflow
208  case 4: break; //conversion time overflow
209  case 6: break; //ADC10HI
210  case 8: break; //ADC10LO
211  case 10: break; //ADC10IN
212  case 12: //ADC10IFG0
213 
214  //Automatically clears ADC10IFG0 by reading memory buffer
215  //ADC10MEM = A0 > 0.5V?
216  if (ADC10_B_getResults(ADC10_B_BASE) < 0x155){
217  //Clear P1.0 LED off
218  /*
219  * Select Port 1
220  * Set Pin 0 to output Low.
221  */
223  GPIO_PORT_P1,
224  GPIO_PIN0
225  );
226 
227  //Clear P3.5 LED off
228  /*
229 
230  * Select Port 3
231  * Set Pin 5 to output Low.
232  */
234  GPIO_PORT_P3,
235  GPIO_PIN5
236  );
237 
238  } else {
239  //Set P1.0 LED on
240  /*
241 
242  * Select Port 1
243  * Set Pin 0 to output high.
244  */
246  GPIO_PORT_P1,
247  GPIO_PIN0
248  );
249 
250  //Set P3.5 LED on
251  /*
252 
253  * Select Port 3
254  * Set Pin 5 to output high.
255  */
257  GPIO_PORT_P3,
258  GPIO_PIN5
259  );
260  }
261 
262  //Clear CPUOFF bit from 0(SR)
263  //Breakpoint here and watch ADC_Result
265  break;
266  default: break;
267  }
268 }
269 
270 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
271 #pragma vector=TIMER0_A0_VECTOR
272 __interrupt
273 #elif defined(__GNUC__)
274 __attribute__((interrupt(TIMER0_A0_VECTOR)))
275 #endif
276 void TA0_ISR (void)
277 {
278  TA0CTL = 0;
279  LPM0_EXIT; // Exit LPM0 on return
280 }
281 
void ADC10_ISR(void)
void main(void)
void TA0_ISR(void)
#define TIMER_PERIOD
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)